package com.UnionPayCenter.service.serviceImpl;


import com.UnionPayCenter.entity.*;
import com.UnionPayCenter.enums.ErrorCodeEnum;
import com.UnionPayCenter.enums.SexEnum;
import com.UnionPayCenter.exception.ApiException;
import com.UnionPayCenter.mapper.*;
import com.UnionPayCenter.service.IIntegralRuleService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
* @Description:
* @Param:
* @return:
* @Author: bite
* @Date: 2019/4/25
*/
@Service
@Transactional
public class IntegralRuleServiceImpl implements IIntegralRuleService {
    @Autowired
    private IntegralRuleMapper ruleMapper;
    @Autowired
    private ManagerMapper managerMapper;

    @Override
    public IntegralRuleEntity findRegistry(JSONObject jsonParam) {
        Integer managerId = jsonParam.getInteger("managerId");
        Integer shopId = jsonParam.getInteger("shopId");
        ManagerEntity managerEntity = managerMapper.selectByShopId(managerId, shopId);
        if (managerEntity != null) {
            IntegralRuleEntity ruleEntity = ruleMapper.selectRegistry(shopId);
            return ruleEntity;
        } else {
            throw new ApiException(ErrorCodeEnum.SW10.code(), String.format(ErrorCodeEnum.SW10.msg(), "managerId"));
        }
    }

    @Override
    public boolean update(JSONObject jsonParam) {
        Integer managerId = jsonParam.getInteger("managerId");
        Integer shopId = jsonParam.getInteger("shopId");
        ManagerEntity managerEntity = managerMapper.selectByShopId(managerId, shopId);
        if (managerEntity != null) {
            Integer ruleId = jsonParam.getInteger("ruleId");
            IntegralRuleEntity ruleEntity = ruleMapper.selectByPrimaryKey(ruleId);
            if(shopId != ruleEntity.getShopId()){
                throw new ApiException(ErrorCodeEnum.SW10.code(), String.format(ErrorCodeEnum.SW10.msg(), "ruleId"));
            }
            Integer isGive = jsonParam.getInteger("isGive");
            Integer number = jsonParam.getInteger("number");
            ruleEntity.setIsGive(isGive);
            ruleEntity.setNumber(number);
            ruleMapper.updateByPrimaryKeySelective(ruleEntity);
            return true;
        } else {
            throw new ApiException(ErrorCodeEnum.SW10.code(), String.format(ErrorCodeEnum.SW10.msg(), "managerId"));
        }
    }
}
